package com.goddess.yeecolor.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.goddess.yeecolor.R;
import com.goddess.yeecolor.activity.myFragmentItemActivityAll.JoinYouyasActivity;
import com.goddess.yeecolor.base.BaseActivity;
import com.goddess.yeecolor.tools.ImageUtils;
import com.goddess.yeecolor.tools.JumpIntents;
import com.goddess.yeecolor.tools.LogUtils;
import com.goddess.yeecolor.tools.MyUrl;
import com.goddess.yeecolor.tools.OkttpUtils;
import com.goddess.yeecolor.tools.SharePreceUtils;
import com.goddess.yeecolor.tools.wxPayActivity;
import com.goddess.yeecolor.view.dialog_progress;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import static com.goddess.yeecolor.R.id.weixin_ll;

/**
 * 作者: CaoJunping
 * 时间: 2017/1/22
 * 作用: ******
 * 备注: 重用----支付页面
 */
public class ReusingRaymentActivity extends BaseActivity {

    @InjectView(R.id.img_top)
    ImageView imgTop;
    @InjectView(R.id.tv_title)
    TextView tvTitle;
    @InjectView(R.id.tv_price)
    TextView tvPrice;
    @InjectView(R.id.btn_join_submit)
    Button btnJoinSubmit;
    @InjectView(R.id.tv_join)
    TextView tvJoin;
    @InjectView(R.id.weixin_img)
    ImageView weixinImg;
    @InjectView(R.id.weixin_ll)
    RelativeLayout weixinLl;
    @InjectView(R.id.zhifubao_img)
    ImageView zhifubaoImg;
    @InjectView(R.id.zhifubao_ll)
    RelativeLayout zhifubaoLl;
    @InjectView(R.id.weixin_icon)
    ImageView weixinIcon;
    @InjectView(R.id.zhifubao_icon)
    ImageView zhifubaoIcon;
    private String typed;
    private Bundle bundle;
    private String order_id;
    private Toolbar mToobar;
    private TextView mTitle;
    private String jsonString;
    private String price;
    private MyBracastReceiver bracastReceiver;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.AppTheme);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reusing_payment);
        broast();
        ButterKnife.inject(this);
        initFindView();
        bundle = getIntent().getBundleExtra("bundle");

        initData();
    }

    private void broast(){
        bracastReceiver=new MyBracastReceiver();
        IntentFilter filter=new IntentFilter();
        filter.addAction("com.zy.brocast");
        registerReceiver(bracastReceiver,filter);
    }
    private boolean sf = true;
    @OnClick({R.id.btn_join_submit, R.id.tv_join, R.id.weixin_ll, R.id.zhifubao_ll})
    public void onClick(View view) {
        switch (view.getId()) {
            case weixin_ll://微信支付单选按钮

                weixinImg.setImageDrawable(getResources().getDrawable(R.mipmap.radio2));
                zhifubaoImg.setImageDrawable(getResources().getDrawable(R.mipmap.radio1));
                sf = true;
                break;
            case R.id.zhifubao_ll://支付宝单选按钮

                zhifubaoImg.setImageDrawable(getResources().getDrawable(R.mipmap.radio2));
                weixinImg.setBackground(null);
                weixinImg.setImageDrawable(getResources().getDrawable(R.mipmap.radio1));
                sf = false;
                break;
            case R.id.btn_join_submit://提交按钮
                if (sf) {
                    if (price!=null){

                        int prices = (int)(Double.parseDouble(price)*100);//价格
                        LogUtils.getInstence().info("价格"+prices);
                        SharePreceUtils.saveString("orders","reus","tag");
                        wxPayActivity.getIntance().pay(title,order_id,prices);
                    }
                    int prices = (int)(Double.parseDouble(price)*100);//价格
                    SharePreceUtils.saveString("orders","reus","tag");
                } else {
                    Intent intent = new Intent(ReusingRaymentActivity.this, LoginAcitivity.class);//支是选中支付宝的支付按钮\
                    startActivity(intent);
                }
                break;
            case R.id.tv_join://底部加入优雅荟文字点击
                OkttpUtils.getIntence().OkttpPost(MyUrl.HUIYUAN, map, handler, 2);

                break;
        }
    }
    private dialog_progress dialog_progress;
    private void setProgress(){
        dialog_progress=new dialog_progress(this);
        dialog_progress.requestWindowFeature(Window.FEATURE_NO_TITLE);

        dialog_progress.setCanceledOnTouchOutside(false);//按返回键可以关闭进度条
        dialog_progress.show();
    }
    @Override
    public void initOnClck() {

    }

    @Override
    public void initFindView() {
        mToobar = (Toolbar) findViewById(R.id.title_toobar);
        mTitle = (TextView) findViewById(R.id.title_content);
        mTitle.setText("课程购买");
        mToobar.setNavigationIcon(R.mipmap.back3x);
        mToobar.setNavigationOnClickListener((view) -> {
            setFiish(this);
        });
    }

    private Map<Object, Object> map;

    @Override
    public void initData() {
        setProgress();
        map=new HashMap<Object, Object>();
        map.put("buy_id",bundle.getString("buy_id"));//购买id
        map.put("unique", SharePreceUtils.getOther("user","unique"));
        map.put("order_id",bundle.getString("order_id"));
        map.put("typed", bundle.getString("type"));
        OkttpUtils.getIntence().OkttpPost(MyUrl.BUY,map,handler,1);
    }

    private String thumb, title,states,state;

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            try {
                jsonString=msg.getData().getString("result");
                JSONObject data=new JSONObject(jsonString);
                JSONObject result=data.getJSONObject("result");
                if (data.getInt("code")==100){
                    if (msg.what==2){
                        state=data.optString("state");
                        Intent intent = new Intent(ReusingRaymentActivity.this, JoinYouyasActivity.class);//支是选中支付宝的支付按钮\
                        intent.putExtra("state",state);
                        startActivity(intent);
                    }
                    else {
                        JSONObject event=result.getJSONObject("events");
                        states=result.optString("state");
                        order_id=result.optString("order_id");
                        if (states.equals("0")){
//                        order_id=result.optString("order_id");
                            title = event.optString("title");
                            tvTitle.setText(title);
                            price = event.optString("price");
                            tvPrice.setText(price);
                            ImageUtils.getInstance().ImageStart(ReusingRaymentActivity.this, event.getString("thumb"), imgTop);
                            btnJoinSubmit.setText("前往支付(" + price + ")");
                            SharePreceUtils.saveOrder(order_id,title,price);

                        }
                        else if (states.equals("1")){//支付成功
                            SharePreceUtils.saveOrder(order_id,title,price);
//                        ToastUtils.setToastText(data.getString("msg"),ReusingRaymentActivity.this);
                            JumpIntents.startIntent(ReusingRaymentActivity.this, EnrollFinishAcitity.class,states);//这是加入优雅荟选中微信的支付按钮/
                            setFiish(ReusingRaymentActivity.this);

                        }
                    }

                }
                if (dialog_progress!=null&&dialog_progress.isShowing()){
                    dialog_progress.dismiss();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    };
    class  MyBracastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String back=intent.getStringExtra("data");
            LogUtils.getInstence().info(back);
            finish();
            overridePendingTransition(0,0);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bracastReceiver!=null){
            unregisterReceiver(bracastReceiver);
        }
    }
}
